<template>
  <div>
    <van-nav-bar title="编辑资料" left-arrow @click-left="$router.go(-1)" />
    <!-- 头像区域 -->
    <div class="upload-box">
      <van-image round width="100" height="100" fit="cover" :src="info.photo" />
      <!-- 上传文件组件 -->
      <van-uploader :after-read="afterRead" />
    </div>
    <van-cell-group>
      <!-- 名称 -->
      <van-cell @click="changeName" title="名称" :value="info.name" is-link />
      <!-- 性别 -->
      <van-cell title="性别" :value="info.gender ? '男' : '女'" is-link />
      <!-- 生日 -->
      <van-cell
        @click="showBirthFn"
        title="生日"
        :value="info.birthday"
        is-link
      />
    </van-cell-group>
    <!-- 修改名称的弹出框 -->
    <van-dialog
      @confirm="updateName"
      v-model="show"
      title="修改名称"
      show-cancel-button
    >
      <van-field ref="txtName" class="txt-name" v-model="editName" />
    </van-dialog>
    <!-- 修改生日的弹出层 -->

    <van-popup
      v-model="isshow"
      round
      position="bottom"
      :style="{ height: '50%' }"
    >
      <!-- 引入的选择日期组件 -->
      <van-datetime-picker
        v-model="currentDate"
        @confirm="updateBirthday"
        type="date"
        title="选择年月日"
        :min-date="minDate"
        :max-date="maxDate"
      />
    </van-popup>
    <!-- 裁剪区域的容器 -->
    <div v-show="cropShow" class="crop-box">
      <vueCropper
        ref="cropper"
        autoCrop
        :img="img"
        autoCropWidth="200"
        autoCropHeight="200"
      ></vueCropper>
      <van-button @click="doCrop" type="primary">裁剪</van-button>
      <van-button style="right:0;" type="primary">取消</van-button>
    </div>
  </div>
</template>

<script>
import moment from 'moment'
import { userApi } from '@/Api'
import { editInfoAPI, editphotoApi } from '@/Api'
import { VueCropper } from 'vue-cropper'

export default {
  components: {
    VueCropper
  },
  data () {
    return {
      cropShow: false,
      isshow: false,
      show: false,
      editName: '',
      minDate: new Date(1950, 0, 1),
      maxDate: new Date(),
      currentDate: new Date(2021, 0, 17),
      img: ''
    }
  },
  methods: {
    // 点击裁剪按钮事件
    doCrop () {
      // 插件的获取截图方式
      this.$refs.cropper.getCropBlob(async data => {
        // do something
        // ajax请求不能直接发送文件,发送文件需要用FromData
        let fm = new FormData()
        fm.append('photo', data)
        // 用裁剪后的文件装进FormData里面然后发请求
        let res = await editphotoApi(fm)
        this.$store.commit('changeuserinfo', {
          ...this.info,
          photo: res.data.data.photo
        })
        this.cropShow = false
      })
    },
    // 上传组件函数
    afterRead (file) {
      // 此时可以自行将文件上传至服务器
      this.cropShow = true
      this.img = file.content
      console.log(file)
    },
    async updateBirthday () {
      const birthday = moment(this.currentDate).format('YYYY-MM-DD')
      await editInfoAPI({ birthday })
      this.isshow = false
      this.$store.commit('changeuserinfo', {
        ...this.info,
        birthday
      })
    },
    showBirthFn () {
      this.isshow = true
      this.currentDate = new Date(this.info.birthday)
    },
    changeName () {
      this.show = true
      this.editName = this.info.name

      setTimeout(() => {
        this.$refs.txtName.focus()
      }, 500)
    },
    async updateName () {
      if (this.editName === '') {
        return this.$toast('请输入名字')
      }
      // 因为服务器返回的数据没什么意义,所以没必要接收
      await editInfoAPI({
        name: this.editName
      })
      this.$store.commit('changeuserinfo', {
        ...this.info,
        name: this.editName
      })
    }
  },
  computed: {
    info () {
      return this.$store.state.userinfo
    }
  },
  async created () {
    if (!this.info.name) {
      let res = await userApi()
      this.$store.commit('changeuserinfo', res.data.data)
    }
  }
}
</script>

<style lang="less" scoped>
.upload-box {
  height: 140px;
  position: relative;
  display: flex;
  justify-content: center;
  align-items: center;
  .van-uploader {
    width: 100%;
    height: 100%;
    position: absolute;
    opacity: 0;
    ::v-deep .van-uploader__wrapper {
      width: 100%;
      height: 100%;
      .van-uploader__upload {
        width: 100%;
        height: 100%;
      }
    }
  }
}
.txt-name {
  border: 1px solid #ddd;
}
.crop-box {
  width: 100vw;
  height: 100vh;
  background-color: red;
  position: fixed;
  left: 0;
  top: 0;
}
.van-button {
  position: absolute;
  bottom: 0;
}
</style>
